package cn.xeblog.plugin.game.ngsnake;

import cn.xeblog.commons.enums.Game;
import cn.xeblog.plugin.annotation.DoGame;
import cn.xeblog.plugin.game.AbstractGame;
import cn.xeblog.plugin.game.ngsnake.model.GameMode;
import cn.xeblog.plugin.game.ngsnake.ui.SnakeGameUI;
import com.intellij.openapi.ui.ComboBox;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 不贪吃蛇
 *
 * @author anlingyi
 * @date 2022/8/5 6:24 PM
 */
@DoGame(Game.NON_GLUTTONOUS_SNAKE)
public class NonGluttonousSnake extends AbstractGame {

  /**
   * 游戏模式
   */
  private GameMode gameMode;

  @Override
  protected void init() {
    // 初始化主面板
    super.initMainPanel();
    mainPanel.setMinimumSize(new Dimension(150, 200));

    // 开始面板
    JPanel startPanel = new JPanel();
    startPanel.setBounds(10, 10, 120, 200);
    mainPanel.add(startPanel);

    // 标题
    JLabel title = new JLabel("不贪吃蛇！");
    title.setFont(new Font("", 1, 14));
    startPanel.add(title);

    Box vBox = Box.createVerticalBox();
    startPanel.add(vBox);

    vBox.add(Box.createVerticalStrut(20));

    // 游戏模式
    JLabel modelLabel = new JLabel("游戏模式：");
    modelLabel.setFont(new Font("", 1, 13));
    vBox.add(modelLabel);
    vBox.add(Box.createVerticalStrut(5));
    ComboBox gameModeBox = new ComboBox();
    // 设置此组件的首选大小
    gameModeBox.setPreferredSize(new Dimension(40, 30));
    // 为下拉列表框添加选项
    for (GameMode value : GameMode.values()) {
      gameModeBox.addItem(value.getName());
    }

    // 默认选中不贪吃模式
    gameMode = GameMode.NON_GLUTTONOUS;
    gameModeBox.setSelectedItem(gameMode.getName());
    // 游戏模式下拉列表框监听
    gameModeBox.addActionListener(l -> {
      GameMode selectedGameMode = GameMode.getMode(gameModeBox.getSelectedItem().toString());
      if (selectedGameMode != null) {
        gameMode = selectedGameMode;
      }
    });
    vBox.add(gameModeBox);

    vBox.add(Box.createVerticalStrut(20));
    // 添加开始游戏按钮
    vBox.add(getStartGameButton());
    // 添加退出按钮
    vBox.add(getExitButton());

    mainPanel.updateUI();
  }

  @Override
  protected void start() {
    // 初始化主面板
    super.initMainPanel();

    int width = 400;
    int height = 300;
    mainPanel.setMinimumSize(new Dimension(width + 10, height + 80));
    // 边界布局
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(Box.createVerticalStrut(5), BorderLayout.NORTH);

    // 初始化游戏面板
    SnakeGameUI snakeGameUI = new SnakeGameUI(width, height, gameMode);
    mainPanel.add(snakeGameUI, BorderLayout.CENTER);

    // 添加穿墙复选框
    JPanel topPanel = new JPanel();
    JCheckBox pierceCheckbox = new JCheckBox("穿墙", false);
    pierceCheckbox.addChangeListener(l -> snakeGameUI.setPierced(((JCheckBox) l.getSource()).isSelected()));
    topPanel.add(pierceCheckbox);
    mainPanel.add(topPanel, BorderLayout.NORTH);

    JPanel buttonPanel = new JPanel();
    // 添加返回游戏按钮
    buttonPanel.add(getBackGameButton());
    // 添加退出按钮
    buttonPanel.add(getExitButton());
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    mainPanel.updateUI();

    // 聚集在游戏面板上
    snakeGameUI.requestFocusInWindow();
  }

  /**
   * 开始游戏按钮
   *
   * @return 开始游戏按钮
   */
  private JButton getStartGameButton() {
    JButton button = new JButton("开始游戏");
    button.addActionListener(e -> start());
    return button;
  }

  /**
   * 返回游戏按钮
   *
   * @return 返回游戏按钮
   */
  private JButton getBackGameButton() {
    JButton button = new JButton("返回游戏");
    button.addActionListener(e -> init());
    return button;
  }

}
